package com.feicheng.controller.shop.admin.system;


import com.feicheng.common.constant.Constant;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.Role;
import com.feicheng.persistence.entity.StoreRole;
import com.feicheng.service.IFSystemService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cailongyang
 * @since 2021-12-02
 */
@RestController("/shop/admin/role")
@RequestMapping("/shop/admin/role")
public class StoreRoleController {

    /* 通过依赖查找实现多实现BEAN的调用 */
    @Resource(name="system-service")
    Map<String, IFSystemService> systemServiceBean;

    @DeleteMapping("/info")
    @Transactional
    public ResultInfo deleteRole(Integer roleId) {
        return systemServiceBean.get(Constant.MODEL_NAME.SHOP).deleteUser(roleId);
    }

    @PostMapping("/info")
    @Transactional
    public ResultInfo addRole(StoreRole role, String permissions) {
        return systemServiceBean.get(Constant.MODEL_NAME.SHOP).addRole(role, permissions);
    }

    @PutMapping("/info")
    @Transactional
    public ResultInfo updateRole(StoreRole role, String permissions) {
        return systemServiceBean.get(Constant.MODEL_NAME.SHOP).updateRole(role, permissions);
    }

    @GetMapping("/info")
    public ResultInfo getRoleById(Integer roleId) {
        return systemServiceBean.get(Constant.MODEL_NAME.SHOP).getRoleById(roleId);
    }

    @GetMapping("/all")
    public ResultInfo all() {
        return systemServiceBean.get(Constant.MODEL_NAME.SHOP).allRoles(true);
    }

    @GetMapping("/list")
    public ResultInfo list(
            String roleName,
            Boolean status,
            @RequestParam(defaultValue="1") Integer currentPage,
            @RequestParam(defaultValue="10")Integer pageSize
    ) {
        return systemServiceBean.get(Constant.MODEL_NAME.SHOP).selectRoles(roleName, status, currentPage, pageSize);
    }
}
